package com.sugon.controller.device;

import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.DevAmPlanEntity;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.device.model.DevMaintenPlanItemVO;
import com.sugon.modules.device.model.vo.CorrelateMaintenancePlanAndRuleParam;
import com.sugon.modules.device.model.vo.DevMaintenancePlanDeviceVO;
import com.sugon.modules.device.model.vo.DevMaintenancePlanItemDateAdjustParamVO;
import com.sugon.modules.device.service.IDeviceAmPlanManager;
import com.sugon.service.DevAmPlanItemService;
import com.sugon.service.DevAmPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 设备维护计划项信息Controller
 *
 * @author YuXD
 * @date 2023-01-30
 */
@Controller
@RequestMapping("devAmPlanItem")
public class DevAmPlanItemController {

    @Resource
    private DevAmPlanItemService devAmPlanItemService;
    @Resource
    private DevAmPlanService devAmPlanService;
    @Resource
    private IDeviceAmPlanManager deviceAmPlanManager;

    @NonBusinessParamValid
    @AutoLog("设备维护计划与规程关联配置表--新增")
    @RequestMapping("/addData")
    @ResponseBody
    public R addData(@Validated @RequestBody CorrelateMaintenancePlanAndRuleParam correlateParam, BindingResult bindingResult) {
        String planId = correlateParam.getPlanId();
        DevAmPlanEntity devPlanMain = devAmPlanService.queryObject(planId);
        if (devPlanMain == null) {
            return R.error("维护计划不存在,请确认");
        }
        // 只可以编辑处于编制中的设备维护计划
        if (!ProcessProgressConst.NEW.equals(devPlanMain.getStatus()) && !ProcessProgressConst.NOT_PASSED.equals(devPlanMain.getStatus())) {
            return R.error("只有处于编制中的计划才可以添加规程");
        }
        String[] ruleIds = correlateParam.getRules().split(",");
        return deviceAmPlanManager.correlateMaintenancePlanAndRule(planId, ruleIds) ? R.ok() : R.error();
    }

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<DevMaintenPlanItemVO> devPlanMainItemList = devAmPlanItemService.queryList(query);
        int total = devAmPlanItemService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devPlanMainItemList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看设备维护计划中指定维护频率下的设备维护项列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/listDev")
    @ResponseBody
    public R listDev(@RequestParam Map<String, Object> params) {
        List<DevMaintenancePlanDeviceVO> devPlanMainItemList = devAmPlanItemService.queryDevList(params);
        PageUtils pageUtil = new PageUtils(devPlanMainItemList, devPlanMainItemList.size(), 1000, 1);
        return R.ok().put("page", pageUtil);
    }

    @AutoLog("调整维护计划中设备维护执行时间")
    @RequestMapping("/updateDay")
    @ResponseBody
    @NonBusinessParamValid
    public R updateDay(@Validated @RequestBody List<DevMaintenancePlanItemDateAdjustParamVO> rules, BindingResult bindingResult) {
        return deviceAmPlanManager.adjustDevMaintenancePlanItem(rules) ? R.ok() : R.error();
    }

}
